<?php


class editRegion extends bee\form
{ protected static $__formID = "frm200911270115";
  public $__caption = "Edit region";
  public $__version = "1.0";
  protected $__activeMenu = "regionOverview";

  protected $region;
  protected $error;
  protected $tId;
  protected $tName;
  protected $tDescription;

  public function createForm($evt)
  { $this->region = new region();
    $this->error = new bee\serror();
    if(isset($evt["id"]))
    { $this->region->SELECT($evt["id"]);
    }
    $this->tId = new bee\text("id", $this->region->id);
    $this->tName = new bee\text("name", $this->region->name);
    $this->tDescription = new bee\text("description", $this->region->description);
  }

  public function drawForm($evt)
  { static::$DSDoc->setRow($this->region);
  }

  public function add($evt)
  { $this->region->fillFromArray($evt);
    if($this->validate($this->region, $this->error))
    { $this->region->INSERT();
      bee\app::setMessage("Region successfully added.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_WARNING);
    }
  }

  public function edit($evt)
  { $this->region->fillFromArray($evt);
    if($this->validate($this->region, $this->error))
    { $this->region->UPDATE();
      bee\app::setMessage("Region successfully updated.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_WARNING);
    }
  }

  public function delete($evt)
  {
    $client = new user_ext();
    $client->region = $this->region->id;
    $rs_client = new bee\rowSet($client);
    if($rs_client->getRow($client))
    { bee\app::setMessage("Region \"" . $this->region->name . "\" can't be deleted becuse is in use.", MSG_LEVEL_WARNING);
    } else
    { $this->region->DELETE();
      bee\app::setMessage("Region \"" . $this->region->name . "\" successfully deleted.");
    }
    header("Location: index.php?__form=" . $this->__parent . "&sys_msg=" . urlencode(bee\app::getMessageText()) . "&sys_msg_style=" . bee\app::getMessageLevel() );
  }

  protected function validate(region $row, bee\serror &$error)
  { if(!$row->name)
    { $error->err_code = -10;
      $error->err_description .= "\nName must be filled!";
      return false;
    }
    $error->err_code = 0;
    return true;
  }
}